package com.neudu.yiyang.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.neudu.yiyang.commons.JSONReturn;
import com.neudu.yiyang.pojo.Nurserecord;
import com.neudu.yiyang.service.NurserecordService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.HashMap;

@RestController
public class NurserecordController {

    private static final Logger logger = LoggerFactory.getLogger(NurserecordController.class);

    @Autowired
    private NurserecordService nurserecordService;

    @Autowired
    private JSONReturn jsonReturn;

    /**
     * 获取所有护理记录信息
     */
    @PostMapping("/get_all_nurserecords")
    public Object getAllNursingRecords(@RequestParam("pageNum") Integer pageNum,
                                       @RequestParam("pageSize") Integer pageSize,
                                       @RequestParam(value = "customerId", required = false) Integer customerId) {
        try {
            PageHelper.startPage(pageNum, pageSize);
            LambdaQueryWrapper<Nurserecord> queryWrapper = new LambdaQueryWrapper<>();
            if (customerId != null) {
                queryWrapper.eq(Nurserecord::getCustomerId, customerId);
            }
            queryWrapper.eq(Nurserecord::getIsDeleted, 0);
            List<Nurserecord> resultList = nurserecordService.list(queryWrapper);
            PageInfo<Nurserecord> pageInfo = new PageInfo<>(resultList);

            Map<String, Object> pageResult = new HashMap<>();
            pageResult.put("pageNum", pageInfo.getPageNum());
            pageResult.put("pageSize", pageInfo.getPageSize());
            pageResult.put("total", pageInfo.getTotal());
            pageResult.put("list", pageInfo.getList());
            return jsonReturn.returnSuccess(pageResult);
        } catch (Exception e) {
            logger.error("获取护理记录失败", e);
            return jsonReturn.returnError("获取护理记录失败：" + e.getMessage());
        }
    }

    /**
     * 根据ID获取护理记录信息
     */
    @PostMapping("/get_nurserecord_by_id")
    public Object getNursingRecordInfo(@RequestParam("id") Integer id) {
        try {
            Nurserecord record = nurserecordService.getById(id);
            if (record == null || record.getIsDeleted() == 1) {
                return jsonReturn.returnError("护理记录不存在");
            }
            return jsonReturn.returnSuccess(record);
        } catch (Exception e) {
            logger.error("获取护理记录信息失败", e);
            return jsonReturn.returnError("获取护理记录信息失败：" + e.getMessage());
        }
    }

    /**
     * 删除护理记录
     */
    @PostMapping("/remove_nurserecord_by_id")
    public Object removeOne(@RequestParam("id") Integer id) {
        try {
            Nurserecord record = nurserecordService.getById(id);
            if (record == null) {
                return jsonReturn.returnError("护理记录不存在");
            }
            record.setIsDeleted(1);
            boolean updateSuccess = nurserecordService.updateById(record);
            if (updateSuccess) {
                return jsonReturn.returnSuccess();
            } else {
                return jsonReturn.returnError("删除护理记录失败");
            }
        } catch (Exception e) {
            logger.error("删除护理记录失败", e);
            return jsonReturn.returnError("删除护理记录失败：" + e.getMessage());
        }
    }

    /**
     * 新增护理记录
     */
    @PostMapping("/add_nurserecord")
    public Object addNursingRecord(@RequestBody Nurserecord nurserecord) {
        try {
            logger.info("接收到的新增护理记录数据: {}", nurserecord);
            nurserecord.setIsDeleted(0);
            boolean saveSuccess = nurserecordService.save(nurserecord);
            if (saveSuccess) {
                logger.info("新增护理记录成功");
                return jsonReturn.returnSuccess();
            } else {
                logger.error("新增护理记录失败，数据: {}", nurserecord);
                return jsonReturn.returnError("新增护理记录失败");
            }
        } catch (Exception e) {
            logger.error("新增护理记录出错", e);
            return jsonReturn.returnError("新增护理记录失败：" + e.getMessage());
        }
    }

    /**
     * 修改护理记录
     */
    @PostMapping("/update_nurserecord")
    public Object updateNursingRecord(@RequestBody Nurserecord nurserecord) {
        try {
            Nurserecord existingRecord = nurserecordService.getById(nurserecord.getId());
            if (existingRecord == null || existingRecord.getIsDeleted() == 1) {
                return jsonReturn.returnError("待修改的护理记录不存在");
            }
            boolean updateSuccess = nurserecordService.updateById(nurserecord);
            if (updateSuccess) {
                return jsonReturn.returnSuccess();
            } else {
                return jsonReturn.returnError("修改护理记录失败");
            }
        } catch (Exception e) {
            logger.error("修改护理记录失败", e);
            return jsonReturn.returnError("修改护理记录失败：" + e.getMessage());
        }
    }

    @PostMapping("/get_nurserecords_by_customer")
    public String getNursingRecordsByCustomer(@RequestBody Map<String, Object> params) {
        try {
            Integer customerId = (Integer) params.get("customerId");
            Integer pageNum = (Integer) params.get("pageNum");
            Integer pageSize = (Integer) params.get("pageSize");

            Page<Nurserecord> page = new Page<>(pageNum, pageSize);
            LambdaQueryWrapper<Nurserecord> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Nurserecord::getCustomerId, customerId);
            queryWrapper.eq(Nurserecord::getIsDeleted, 0);

            Page<Nurserecord> nurserecordPage = nurserecordService.page(page, queryWrapper);
            Map<String, Object> result = new HashMap<>();
            result.put("result", "success");
            result.put("pageNum", nurserecordPage.getCurrent());
            result.put("pageSize", nurserecordPage.getSize());
            result.put("total", nurserecordPage.getTotal());
            result.put("list", nurserecordPage.getRecords());
            return jsonReturn.returnSuccess(result);
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError("查询护理记录失败：" + e.getMessage());
        }
    }
}